--- title: "L2-028 秀恩爱分得快" created: 2025-11-28 tags: - 算法 --- # L2-028 秀恩爱分得快 ## 题目 [L2-028 秀恩爱分得快](https://pintia.cn/problem-sets/994805046380707840/exam/problems/type/7?problemSetProblemId=994805054698012672&page=1) ![[image-94b27a6f.png]] ## 思路分析 pat的模拟题输出太恶心了 过不了全部 不管了 [题解](https://www.liuchuo.net/archives/5646) ![[image-a04285f5.png]] ![[image-5b2b519a.png]] ## 代码实现 ```cpp #include using namespace std; #define endl '\n' using ll = long long; using ull = unsigned long long; using PII = pair; using Pll = pair; int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n,m;cin>>n>>m; unordered_map> rela; while(m--){ int num;cin>>num; vector people(num); for(int i=0;i>people[i]; } for (int i=0;i> A >> B; double maxA = 0; vector listA; for (auto &p : rela[A]) { if ((A > 0 && p.first < 0) || (A < 0 && p.first > 0)) { if (p.second > maxA) { maxA = p.second; listA.clear(); listA.push_back(p.first); } else if (p.second == maxA) { listA.push_back(p.first); } } } double maxB = 0; vector listB; for (auto &p : rela[B]) { if ((B > 0 && p.first < 0) || (B < 0 && p.first > 0)) { if (p.second > maxB) { maxB = p.second; listB.clear(); listB.push_back(p.first); } else if (p.second == maxB) { listB.push_back(p.first); } } } bool isBestCouple = (maxA == rela[A][B]) && (maxB == rela[B][A]); if (isBestCouple) { cout << A << " " << B << endl; } else { sort(listA.begin(), listA.end(), [](int x, int y) { return abs(x) < abs(y); }); sort(listB.begin(), listB.end(), [](int x, int y) { return abs(x) < abs(y); }); for (int v : listA) cout << A << " " << v << endl; for (int v : listB) cout << B << " " << v << endl; } return 0; } ``` ## 同类题型 ## 视频讲解 --- ⬅️ [[L2-027 名人堂与代金券|L2-027 名人堂与代金券]] 🏠 [[00-天梯赛]] ➡️ [[L2-029 特立独行的幸福|L2-029 特立独行的幸福]]